﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace Diplomova_prace
{
    public static class Html
    {
        public static string pathToMathjax = Directory.GetCurrentDirectory();
        public const string MainDiv = "main";
        public const string DivId = "id='"+MainDiv+"'";
        public const string SpanId = "id='text'";
        public const string HtmlLinebreaking = "'HTML-CSS': {linebreaks: { automatic: true, width: '95% container' }}";
        public const string SvgLinebreaking = "SVG: {linebreaks: { automatic: true, width: '100% container' }}";
        public const string MathmlConfigure = "TeX-AMS-MML_HTMLorMML";
        public const string SvgConfigure = "TeX-AMS-MML_SVG";
        public const string mathjaxConfig = HtmlLinebreaking;
        public const string style = @".MathJax_Display{margin:-3px !important; } #" + MainDiv + "{font-size: 24px} ";
        public const string javaScript = @"function changeSize(size) 
		{
          var myeqn = document.getElementById('" + MainDiv + @"');
          myeqn.style.fontSize = size;
          MathJax.Hub.Queue(
            ['Rerender',MathJax.Hub,'myeqn']
          );
        }
        function IsCompleted()
		    { var result = false;
			    MathJax.Hub.Register.StartupHook('End',function () {
				    result = true;
				    });	
                return result;	
		    }
        function WidthDiv()
            {
                var biggest = 0;
                var nodes = document.getElementById('" + MainDiv + @"').childNodes;
                for(var i= 0; i<nodes.length; i++) 
                {
                    if (nodes[i].nodeName.toLowerCase() == 'div') 
                    {
                        if ( biggest < nodes[i].scrollWidth)
                        {
                                biggest = nodes[i].scrollWidth;
                        }
                    }
                }
                            return biggest;
            }
        function HeightDiv()
            {
var sum = 0;
var nodes = document.getElementById('" + MainDiv + @"').childNodes;
for(var i= 0; i<nodes.length; i++) {
if (nodes[i].nodeName.toLowerCase() == 'div') {
        sum = sum + nodes[i].scrollHeight;
    }
}
            return sum;
          }
        ";
        public static string HeaderHTML(string title,string scriptMathjax="",string scriptJavaScript="",string Css="")
        {
            return @"<!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Transitional//EN' 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd'>
                            <html xmlns='http://www.w3.org/1999/xhtml'>
                            <head>
                                <meta http-equiv='content-type' content='text/html; charset=utf-8'>" + Title(title) + JavaScript(scriptJavaScript) + MathJaxConfig(scriptMathjax + mathjaxConfig) + CssStyle(style + Css) +
                          @"</head>"+Body+Div(DivId);
        }
        
        public static string EndHTML
        {
            get { return EndDiv+EndBody+"</html>"; }
        }
        
        private static string Title(string title)
        {
            return "<title>" + title + "</title>";
        }
        
        private static string CssStyle(string style)
        {
            return "<style>" + style + "</style>";
        }
        
        private static string MathJaxConfig(string config)
        {
            return "<script type='text/x-mathjax-config'>MathJax.Hub.Config({" + config + "});</script>";
        }
        /*
         * MathJax.Hub.Queue(['Typeset',MathJax.Hub]); 
         * MathJax.Hub.Config({messageStyle: 'none'});
         */
        private static string JavaScript(string script)
        {
            return "<script>" + javaScript + script + "</script>" + "<script type='text/javascript'src='" + pathToMathjax + @"/mathjax2.3/MathJax.js?config="+MathmlConfigure+"'></script>";
        }
        
        public static string Div(string attribute = "")
        {
            return "<div " + attribute + ">";
        }
        
        public static string EndDiv
        {
            get { return "</div>"; }
        }
        
        public static string Body
        {
            get { return "<body>"; }
        }
        
        public static string EndBody
        {
            get { return "</body>"; }
        }
        
        public static string Span(string input, string attribute = null)
        {
            return "<span " +SpanId +attribute + ">" + input + "</span>";
        }
        
        public static string Br
        {
            get { return "<br/>"; }
        }
        public static string Cursive(string input)
        {
            return "<i>" + input + "</i>";
        }
        public static string Bold(string input)
        {
            return "<b>" + input + "</b>";
        }
    }
}
